package com.example.broad;

import androidx.appcompat.app.AppCompatActivity;

import android.app.PictureInPictureParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.Rational;

public class ReturnDesktopActivity extends AppCompatActivity {

    private DesktopRecevier desktopRecevier;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return_desktop);
        desktopRecevier = new DesktopRecevier();
        IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        registerReceiver(desktopRecevier,filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(desktopRecevier);
    }

    /**
     * @param
     * @return author snjgm
     * time 2022/7/25 下午1:48
     * @description 进入画中画模式或退出画中画模式时触发
     */
    @Override
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
        if (isInPictureInPictureMode) {
            Log.d("TAG", "进入画中画模式了");
        } else {
            Log.d("TAG", "退出画中画模式了");
        }
    }

    /**
     * @param
     * @description 定义一个返回到桌面打广播接收器, 暂时估计只能在本个activity中使用, 单独创建应该导入相对应的画中画包
     * @return author snjgm
     * time 2022/7/25 下午1:47
     */
    public class DesktopRecevier extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra("reason");
                if (!TextUtils.isEmpty(reason) &&
                        (reason.equals("homekey") || reason.equals("recentapps"))) {
                    //android8.0之后才有画中画模式
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
                            && isInPictureInPictureMode()) {
                        //创建画中画模式的参数构建器
                        PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder();
                        //下面的10/5=2,表示画中画窗口的宽度是高度的两倍
                        Rational rational = new Rational(10, 5);
                        builder.setAspectRatio(rational);
                        //进入画中画模式
                        enterPictureInPictureMode(builder.build());
                    }
                }
            }
        }
    }

}